/*
 *
 * Copyright (c) 2017 Ericsson AB Corporation, all rights reserved.
 *
 */

package com.roy.spring.jpa.controller;


import com.roy.spring.jpa.domain.Order;
import com.roy.spring.jpa.domain.OrderRepository;
import com.roy.spring.jpa.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 *
 *
 * @author elloxxn
 * 
 */
@Controller
public class OrderController {

    @Autowired
    private OrderRepository orderRepository;

    @Autowired
    private OrderService orderService;

    @RequestMapping(path = "/orders", method= RequestMethod.POST)
    public ResponseEntity<?> createSampleOrder(@RequestBody Order order) {
        order.setCreateTime(new Date());
        Order storeOrder = orderService.save(order);
        return ResponseEntity.ok(storeOrder);
    }

    @RequestMapping(path = "/orders/{id}", method= RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<Order> createSampleOrder(@PathVariable("id") Long id) {
        Order order = orderRepository.findOne(id);
        return ResponseEntity.ok(order);
    }
}
